home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / actlib13.zip / STRINGS.ZIP / ALIGN.C next >
Text File  |  1993-01-14  |  2KB  |  58 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4.  
  5. /***
  6.  *  Function    :  stralign
  7.  *
  8.  *  Description :  Copy an input string in an output string
  9.  *           with specified alignement (blank padding).
  10.  *
  11.  *  Decisions   :  If given length < 0 returns an empty string.
  12.  *
  13.  *  Parameters  :  out  char        *out_str     result
  14.  *                 in   char        *in_str      in string
  15.  *                 in   align_type  type,     type
  16.  *                 in   int         length       length to be copied
  17.  *
  18.  *  Value       :  type = { align_left, align_center, align_right }
  19.  *
  20.  *  Return code :   pointer to result.
  21.  *
  22.  *  OS/Compiler :   All
  23.  */
  24.  
  25. char *stralign( char *out_str, const char *in_str, align_type type, int length )
  26.  
  27. { int diff;
  28.   char *ptr = out_str;
  29.  
  30.   if ( length <= 0 ) { *out_str = '\0';
  31.                  return out_str;
  32.              }
  33.  
  34.   if ( (diff = (length - strlen(in_str)) ) <= 0 )
  35.      return strleft( out_str, in_str,diff );
  36.  
  37.   switch( type ) { case align_left  : while ( *out_str++ = *in_str++ );
  38.                                   out_str --;
  39.                               for (; diff; diff -- ) *out_str++ = ' ';
  40.                               *out_str++ = '\0';
  41.                        break;
  42.  
  43.            case align_right : for (; diff; diff -- ) *out_str++ = ' ';
  44.                        while ( *out_str++ = *in_str++ );
  45.                        break;
  46.  
  47.                   case align_center: { int lim = diff / 2;
  48.                                   for (; diff > lim; diff -- ) *out_str++ = ' ';
  49.                                 while ( *out_str++ = *in_str++ ); out_str --;
  50.                                 for (; diff; diff -- ) *out_str++ = ' ';
  51.                                 *out_str++ = '\0';
  52.                                 break;
  53.                               }
  54.              }
  55.  
  56.   return ptr;
  57. }
  58.